package com.snow.fvmusic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.snow.fvmusic.bo.CommentAndGreatParam;
import com.snow.fvmusic.bo.WebConst;
import com.snow.fvmusic.component.SingletonThreadPool;
import com.snow.fvmusic.dao.ArticleVODao;
import com.snow.fvmusic.dao.MusicPoolDtoDao;
import com.snow.fvmusic.generator.entities.Comment;
import com.snow.fvmusic.generator.mapper.CommentMapper;
import com.snow.fvmusic.service.AsynchTaskService;
import com.snow.fvmusic.service.MessageService;
import com.snow.fvmusic.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author ：snow
 * @Date ：Created in 2020-10-14
 * @Description：
 * @Modified By：
 * @Version:
 */
@Service
@Slf4j
public class AsynchTaskServiceImpl implements AsynchTaskService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ArticleVODao articleVODao;
    @Resource
    private MessageService messageService;
    @Resource
    private RedisService redisService;
    @Resource
    private MusicPoolDtoDao musicPoolDtoDao;
    private final ThreadPoolExecutor poolExecutor = SingletonThreadPool.getInstance();
    @Override
    public void commitComment(CommentAndGreatParam param) {
        Runnable r = () -> {
            Comment comment = new Comment();
            comment.setArticleId(param.getArticleId());
            comment.setContent(param.getContent());
            comment.setLevel(0);
            comment.setCreateDatetime(LocalDateTime.now());
            comment.setUserId(param.getUserId());
            comment.setParentId(0L);
            commentMapper.insert(comment);
            articleVODao.incrCommentCount(comment.getArticleId());
            messageService.addMessages(param);
        };
        poolExecutor.submit(r);
    }

    @Override
    public void commitGreat(CommentAndGreatParam param) {
        poolExecutor.submit(() -> {
            messageService.addMessages(param);
        });
    }

    @Override
    public void incrArticlesViews(List<Long> articleIds, HttpServletRequest request) {
        Runnable r = () ->{
            String key = WebConst.REDIS_DATA_BASE + "." + WebConst.ARTICLE_VIES_REDIS_KEY + ".article_id." + articleIds.get(0);
            Object[] ip = new String[1];
            ip[0] = request.getHeader("X-Real-IP");
            Long insert = redisService.sAdd(key,WebConst.REDIS_STATISTIC_VIEWS_EXPIRED, ip);
            if (insert > 0){
                articleVODao.incrViewsBatch(articleIds);
            }
        };
        poolExecutor.submit(r);
    }

    @Override
    public void incrMusicPoolsViews(Long poolId, HttpServletRequest request) {
        poolExecutor.submit(() ->{
           String redisKey = WebConst.REDIS_DATA_BASE + "." + WebConst.POOL_VIEWS_USER_KEY + poolId;
           Object[] ip = new String[1];
           ip[0] = request.getHeader("X-Real-IP");
            Long insert = redisService.sAdd(redisKey, WebConst.REDIS_STATISTIC_VIEWS_EXPIRED, ip);
            if (insert > 0){
                log.info("update pool views");
                musicPoolDtoDao.incrPoolsViews(Collections.singletonList(poolId));
            }
        });
    }
}
